package com.yyfax.ph.personal.authority.register;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

public class RegisterModel extends Model {
	private static final String TAG = RegisterModel.class.getSimpleName();

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.register(token, (Params) params);
	}

	@Override
	public void onNext(Object obj) {

		CLog.i(TAG, "onNext: " + obj.toString());
		RegisterResult registerResult = cast(obj);
		EventAgent.post(registerResult);
	}

	private interface Service {
		@POST("sys/user/register")
		Observable<RegisterResult> register(@Header("token") String token, @Body Params params);
	}

	static class Params {
		public String loginName;
		public String password;
		public String invitCode;
		public String verifyCode;
	}

	static class RegisterResult extends BaseResultEntity<RegisterBean> {
	}

	public static class RegisterBean {
		/***
		 * 用户推荐类型 0:公司类型 1:邀请码类型
		 */
		public String type;
		/***
		 * code全局码（10008：验证码错误）
		 */
		public String code;
	}
}
